package com.wechat.service.account.starter.accountManage;

import com.alibaba.fastjson.JSONObject;
import com.wechat.service.account.dto.accountManage.Long2ShortLinkReqDTO;
import com.wechat.service.account.starter.AccessTokenService;
import com.wechat.service.account.starter.BaseWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author yanggld
 * @Date 2019/7/10-17:49
 * @Description
 * 长链接转短链接接口
 */
@Service
public class Long2ShortLinkService extends BaseWechatService {


	public String toShort(Long2ShortLinkReqDTO reqDTO){
		reqDTO.setAccess_token(accessTokenService.getAccessToken());
		ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(buildShortUrl(), reqDTO, JSONObject.class);
		JSONObject body = responseEntity.getBody();
		return body.getString("short_url");
	}

	public String buildShortUrl(){
		return "https://api.weixin.qq.com/cgi-bin/shorturl?access_token="+accessTokenService.getAccessToken();
	}
}
